//
//  KLPopView.m
//  Koala
//
//  Created by cao pan on 11/23/14.
//  Copyright (c) 2014 Anonymous. All rights reserved.
//

#import "KLPopView.h"

@implementation KLPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor clearColor]];
        [self background];
        [self items];
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]] ;
    }
    return self;
}


- (void)background
{
    UIImageView * bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, self.width,200)];
    [bg setImage:[[UIImage imageNamed:@"bg_pop"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]];
    [self addSubview:bg];
}


- (void)items
{
    NSArray * titleArr = @[@"新闻",@"豆壳",@"MIT",@"TIME"];
    
    float buttonHeight = 40;
    for (int i = 0 ; i < 4; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(self.origin.x + 5, (buttonHeight + 5 ) * i, self.width - 5 *2, buttonHeight)];
        [button setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [[button titleLabel] setFont:[UIFont systemFontOfSize:17.0f]];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -200, 0, 0)];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(itemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"menu_%d",i+1]] forState:UIControlStateNormal];
        [self addSubview:button];
    }
}




- (void)itemClicked:(id)sender
{
    UIButton * button = (UIButton *)sender;
    NSUInteger index = button.tag;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedPopIndex:)]) {
        [self.delegate didSelectedPopIndex:index];
    }
}


- (void)show
{
    [UIView animateWithDuration:0.5 animations:^{
        [self setOrigin:CGPointMake(0,20)];
        [self setAlpha:1];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.5 animations:^{
        [self setOrigin:CGPointMake(0 , 200)];
        [self setAlpha:0];
    } completion:^(BOOL finished) {
        
    }];
}



@end
